package com.sip.stream.utils.zfy;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.sip.stream.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PrevieDrawView extends View {
    private static final String TAG = "PrevieDrawView";

    private static final int WHAT_REFRESH = 0;
    private Handler backgroundHandler;
    private HandlerThread backgroundThread;
    private HashMap<String, PreviewDetectedBean> detectedBeans;
    private boolean isClear;
    long lastRefresh;
    private Paint paint;
    private float scale;
    private List<String> timeoutBean;

    public PrevieDrawView(Context context) {
        this(context, null);
        setUp();
    }

    public PrevieDrawView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
        setUp();
    }

    public PrevieDrawView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.scale = 1.0f;
        this.isClear = true;
        this.timeoutBean = new ArrayList();
        this.lastRefresh = 0L;
        setUp();
    }

    private synchronized Handler getBackgroundHandler() {
        if (this.backgroundThread == null || this.backgroundHandler == null) {
            HandlerThread handlerThread = new HandlerThread(TAG);
            this.backgroundThread = handlerThread;
            handlerThread.start();
            this.backgroundHandler = new Handler(this.backgroundThread.getLooper(), new Handler.Callback() {
                @Override
                public  boolean handleMessage(Message message) {
                    if (message.what != 0) {
                        return false;
                    }
                    refreshDraw();
                    return false;
                }
            });
        }
        return this.backgroundHandler;
    }


    private void setUp() {
        this.detectedBeans = new HashMap<>();
        Paint paint = new Paint();
        this.paint = paint;
        paint.setColor(getResources().getColor(R.color.preview_draw));
        this.paint.setStyle(Paint.Style.STROKE);
        this.paint.setStrokeWidth(1.0f);
    }

    public void refreshDraw() {
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - this.lastRefresh > 500) {
            postInvalidate();
            this.lastRefresh = currentTimeMillis;
        }
        getBackgroundHandler().removeMessages(0);
        getBackgroundHandler().sendEmptyMessageDelayed(0, 1000L);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        long currentTimeMillis = System.currentTimeMillis();
        this.timeoutBean.clear();
        for (Map.Entry<String, PreviewDetectedBean> entry : this.detectedBeans.entrySet()) {
            Rect rect = entry.getValue().getRect();
            if (rect == null) {
                return;
            }
            canvas.drawRect(rect, this.paint);
            if (currentTimeMillis - entry.getValue().getTimeMillis() > 1000) {
                this.timeoutBean.add(entry.getKey());
            }
        }
        for (String str : this.timeoutBean) {
            this.detectedBeans.remove(str);
        }
    }

    public void clear() {
        getBackgroundHandler().removeMessages(0);
        this.lastRefresh = 0L;
        this.detectedBeans.clear();
        postInvalidate();
    }

    public void addDetectedBean(PreviewDetectedBean previewDetectedBean) {
        Rect rect = previewDetectedBean.getRect();
        previewDetectedBean.setRect(new Rect((int) (rect.left * this.scale), (int) (rect.top * this.scale), (int) (rect.right * this.scale), (int) (rect.bottom * this.scale)));
        this.detectedBeans.put(previewDetectedBean.getTrackId(), previewDetectedBean);
        refreshDraw();
    }

    public void setPreviewWidth(int i) {
        Log.i(TAG, "setPreviewWidth " + i + " this.getWidth() " + getWidth());
        this.scale = getWidth() / i;
    }
}
